/**
 * @Author: Bobby
 * @Date: 2022-02-15 13:14:17
 * @LastEditTime: 2022-03-03 11:32:37
 * @LastEditors: Bobby
 * @Description:
 * @FilePath: \wms-v2\src\project\bobby\store\preview.js
 * @CIMC-V2
 */
//  getModel,
import { arrayUpdate, recursiveList } from '@ve/utils/utils'
import isEmpty from 'lodash/isEmpty'
import cloneDeep from 'lodash/cloneDeep'
/**
 * @description 预览页
 */
const state = () => ({
  dev: true,
  nowPageId: '', //当前页面
  hasPreview: false, //是否为预览模式
  pages: {}, //所有页面记录进去
  pageCaches: {}, //缓存最原始pagejson
  paths: {},
  pagesMods: {},
  pageModuleRefs: {}, // 页面组件实例的记录
})
//页面模块
const setPagesMods = (pageData) => {
  let obj = {}
  let _getNowPageData = recursiveList([pageData])
  let _arr = []
  _getNowPageData.forEach((v) => {
    _arr = [..._arr, ...v.pageModules]
    obj[v._id] = v
  })
  const flatData = (data, parentId = undefined) => {
    return data.reduce((prev, curr) => {
      if (parentId != undefined) {
        Object.assign(curr, {
          parentId: parentId,
        })
      }
      prev.push(curr)
      if (curr.list && curr.list.length > 0) {
        prev.push(...flatData(curr.list, curr._id))
      }
      return prev
    }, [])
  }

  let flated = flatData(_arr)

  flated.forEach((v) => {
    obj[v._id] = v
  })
  return obj
}

const getters = {
  getDev: (state) => state.dev,
  getNowPageId: (state) => state.nowPageId,
  getHasPreview: (state) => state.hasPreview,
  getPaths: (state) => state.paths,
  getPageData: (state) => (id) => {
    return state.pages[id] || {}
  },
  getNowPageData: (state) => {
    if (!state.nowPageId) {
      return []
    }
    let pageData = state.pages[state.nowPageId]
    return pageData
  },
  getNowPageCacheData: (state) => {
    if (!state.nowPageId) {
      return []
    }
    let pageData = state.pageCaches[state.nowPageId]
    return pageData
  },
  getCurrentPageData: (state) => {
    return (pageId) => {
      return state.pages[pageId] || []
    }
  },
  getPageModuleRefs: (state) => state.pageModuleRefs,

  //获取模块or图层信息
  getDeepModelOrLayerInfo: (state, getters) => {
    return ({ id, type, dataKey }) => {
      if (isEmpty(id) || type === 0 || isEmpty(getters.getNowPageData)) {
        return {}
      }

      // let _layerList = recursiveList([getters.getNowPageData])
      // let _modelInfo = {}
      // if (type === 1) {
      //   //组件
      //   for (let i = 0; i < _layerList.length; i++) {
      //     _modelInfo = getModel(_layerList[i]['pageModules'], id)
      //     if (!isEmpty(_modelInfo)) {
      //       break
      //     }
      //   }
      // }

      // if (type === 2) {
      //   //页面
      //   _modelInfo = getModel(_layerList, id)
      // }
      let _modelInfo = state.pagesMods[state.nowPageId][id]
      return isEmpty(dataKey) ? _modelInfo : _modelInfo[dataKey]
    }
  },
  //获取模块or图层信息
  getDeepModelOrLayerInfoByCurrentPage: (state, getters) => {
    // console.log('getDeepModelOr-LayerInfoByCurrentPage')
    return ({ id, type, dataKey, pageId }) => {
      if (isEmpty(id) || type === 0 || isEmpty(getters.getCurrentPageData(pageId))) {
        return {}
      }
      // let _layerList = recursiveList([getters.getCurrentPageData(pageId)])
      // let _modelInfo = {}
      // if (type === 1) {
      //   //组件
      //   for (let i = 0; i < _layerList.length; i++) {
      //     _modelInfo = getModel(_layerList[i]['pageModules'], id)
      //     if (!isEmpty(_modelInfo)) {
      //       break
      //     }
      //   }
      // }
      // if (type === 2) {
      //   //页面
      //   _modelInfo = getModel(_layerList, id)
      // }
      let _modelInfo = state.pagesMods[pageId][id]

      return isEmpty(dataKey) ? _modelInfo : _modelInfo[dataKey]
    }
  },
}
const mutations = {
  setDev(state, dev) {
    state.dev = dev
  },
  setNowPageId(state, id) {
    state.nowPageId = id
  },
  setPagesMods(state, id) {
    // //设置页面模块
    state.pagesMods[state.nowPageId] = setPagesMods(state.pages[id])
  },
  setPageData(state, data) {
    state.pages[data._id] = data
    // state.pageCaches[data._id] = cloneDeep(data)
  },
  setPageCaches(state, data) {
    state.pageCaches[data._id] = cloneDeep(data)
  },
  setPathsData(state, data) {
    state.paths[data.key] = data.value
  },
  setHasPreview(state, val) {
    state.hasPreview = val
  },
  setNowPageModules(state, data) {
    state.pages[state.nowPageId]['pageModules'] = data
  },
  updateModelInfo(state, { id, modelInfo }) {
    // console.log('进 A TO B ---- updateModelInfo')
    let getPageModules = state.pages[state.nowPageId]['pageModules']
    let arr = arrayUpdate(getPageModules, id, '_id', 'list', modelInfo)
    this.commit('preview/setNowPageModules', arr)
  },
  // 初始化页面Json
  initPages(state) {
    state.nowPageId = ''
    state.pages = {}
    state.pageCaches = {}
    state.paths = {}
    state.pagesMods = {}
  },
  setPageModuleRef(state, data) {
    state.pageModuleRefs[data.moduleId] = data.moduleRef
  },
}
const actions = {
  setDev({ commit }, dev) {
    commit('setDev', dev)
  },
  setNowPageId({ commit }, id) {
    commit('setNowPageId', id)
  },
  setPagesMods({ commit }, id) {
    commit('setPagesMods', id)
  },
  setPageData({ commit }, data) {
    commit('setPageData', data)
  },
  setPageCaches({ commit }, data) {
    commit('setPageCaches', data)
  },
  setPathsData({ commit }, data) {
    commit('setPathsData', data)
  },
  //设置预览弹框显示
  setHasPreview({ commit }, val) {
    commit('setHasPreview', val)
  },
  // 更新模块信息
  updateModelInfo({ commit }, data) {
    //data = {id,data}
    commit('updateModelInfo', data)
  },
  // 初始化页面JSON
  initPages({ commit }) {
    commit('initPages')
  },
  setPageModuleRef({ commit }, data) {
    commit('setPageModuleRef', data)
  },
}
export default { state, getters, mutations, actions }
